<template>
	<view class="uni-container">
		<view class="uni-hello-text">
			<text class="hello-text">打印机测试</text>
		</view>

		<view class="uni-panel">
			<view class="uni-panel-h" @click="applyPermission">
				<text class="uni-panel-text">申请权限</text>
			</view>
		</view>

		<view class="uni-panel">
			<view class="uni-panel-h" @click="hasAllPermission">
				<text class="uni-panel-text">是否拥有全部权限</text>
			</view>
		</view>

		<view class="uni-panel">
			<view class="uni-panel-h" @click="getAllUsbDevices">
				<text class="uni-panel-text">获取所有USB设备</text>
			</view>
		</view>

		<view class="uni-panel">
			<view class="uni-panel-h" @click="setPrinter">
				<text class="uni-panel-text">设置打印机</text>
			</view>
		</view>

		<view class="uni-panel">
			<view class="uni-panel-h" @click="getPrinterAbility">
				<text class="uni-panel-text">获取打印机能力</text>
			</view>
		</view>

		<view class="uni-panel">
			<view class="uni-panel-h" @click="configPrinter">
				<text class="uni-panel-text">打印机配置</text>
			</view>
		</view>

		<view class="uni-panel">
			<view class="uni-panel-h" @click="addPrintFile">
				<text class="uni-panel-text">添加打印的图片资源</text>
			</view>
		</view>

		<view class="uni-panel">
			<view class="uni-panel-h" @click="startPrint">
				<text class="uni-panel-text">开始打印</text>
			</view>
		</view>

		<view class="uni-panel">
			<view class="uni-panel-h" @click="clearPrintQueue">
				<text class="uni-panel-text">清空打印队列</text>
			</view>
		</view>

		<view class="uni-panel">
			<view class="uni-panel-h" @click="cancelPrint">
				<text class="uni-panel-text">取消打印</text>
			</view>
		</view>
	</view>
</template>
<script>
	const printerModule = uni.requireNativePlugin("PrinterModule")
	const toastModule = uni.requireNativePlugin("ToastModule")

	export default {
		data() {
			return {
				list: [{
					id: 'ext-module',
					name: '打印机测试',
					open: false,
					url: '/pages/sample/ext-module'
				}],
				navigateFlag: false
			}
		},
		onLoad() {},
		methods: {
			/**
			 * 申请权限
			 */
			applyPermission() {
				console.log('浏览器准备申请权限')

				printerModule.requestPermissions();
			},

			/**
			 * 是否有全部权限
			 */
			hasAllPermission() {
				const result = printerModule.hasAllPermission();

				console.log('是否有全部权限：' + JSON.stringify(result))

				toastModule.showLongToast(result)
			},

			/**
			 * 获取所有的USB设备
			 */
			getAllUsbDevices() {
				const result = printerModule.getAllUsbDevice();

				console.log('获取所有的USB设备：' + JSON.stringify(result));

				toastModule.showToast(result);
			},

			/**
			 * 设置打印机
			 */
			setPrinter() {
				const result = printerModule.handleSetPrinter({
					usbDeviceId: 1002
				})

				console.log('打印机设置结果：' + JSON.stringify(result));

				toastModule.showToast(result);
			},

			/**
			 * 获取打印机能力
			 */
			getPrinterAbility() {
				const result = printerModule.getPrinterAbility();

				console.log('获取打印机能力结果：' + JSON.stringify(result));

				toastModule.showToast(result);
			},

			/**
			 * 打印机配置
			 */
			configPrinter() {
				const result = printerModule.configPrinter({
					numOfCopies: 1,
					mode: "bw",
					duplex: "simplex",
					collate: "collate",
					paper: "A4",
					feeder: "auto"
				})

				console.log("打印机配置结果：" + JSON.stringify(result));

				toastModule.showToast(result);
			},

			/**
			 * 添加打印的图片资源
			 */
			addPrintFile() {
				const result = printerModule.addPrintFile({
					fileBase64: ''
				});
				
				console.log("添加打印文件结果：" + JSON.stringify(result));
				
				toastModule.showToast(result);
			},

			/**
			 * 开始打印
			 */
			startPrint() {
				const result = printerModule.startPrint();

				console.log("打印结果：" + JSON.stringify(result));

				toastModule.showToast(result);
			},

			/**
			 * 清空打印队列
			 */
			clearPrintQueue() {
				const result = printerModule.clearPrintQueue();

				console.log('清空打印队列结果：' + JSON.stringify(result))
			},

			/**
			 * 取消打印
			 */
			cancelPrint() {
				const result = printerModule.cancelPrint();

				console.log("取消打印结果：" + JSON.stringify(result));

				toastModule.showToast(result);
			},
		}
	}
</script>

<style>
</style>